//
//  AppDelegate.swift
//  Weibo
//
//  Created by 周先生 on 15/11/8.
//  Copyright © 2015年 周先生. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        
        let account = UserAccount.loadAccount()
        print(account)
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.makeKeyAndVisible()
        window?.backgroundColor = UIColor.whiteColor()
        
        //设置主题颜色
        setThemeColor()
        
        
//        isNewVersion() 不要在使用之前调用
        //注册通知
        registerNotification()
//        registerNoti
        
        //判断是否为新版本
        print("-----------")
//        print(isNewVersion())
        //设置跟视图控制器
//        window?.rootViewController = WelcomeViewController()
//        window?.rootViewController = NewFeatureCollectionViewController()
//        window?.rootViewController = MainViewController()
        window?.rootViewController = defaultRootViewController()
//        window?.rootViewController = PictureSelectorViewController()

//        let nav = UINavigationController(rootViewController: ComposeViewController())
//        window?.rootViewController = nav
        return true
    }

    //设置主题颜色
    private func setThemeColor() {
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
    //监听通知
    private func registerNotification() {
    
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchToRootVC:", name: AppSwitchRootViewController, object: nil)

    }
    //跳转到MainVC
   @objc private func switchToRootVC(n: NSNotification) {
        print(123)
        print(n)
        if (n.object as? String) != nil {
            window?.rootViewController = WelcomeViewController()
            return
            
        }
        window?.rootViewController = MainViewController()
    }
    
    //移除通知  编码规范 没什么用
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
        
    }
    //判断用户是否登录 并且是否是新版本
    private func defaultRootViewController() -> UIViewController {
        if UserAccountViewModel().userLogin {
            print("welcome")

            return isNewVersion() ? NewFeatureCollectionViewController() : WelcomeViewController()
        }
        
        print("main")
        return MainViewController()
    }
    //判断是否为新版本
    private func isNewVersion() -> Bool {
        //比较新旧版本
        //获取新版本
        let versionStr = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let versionNum = Double(versionStr)
        //获取旧版本 一般存放在沙盒
        let key = "sandBoxKey"
        let userDefaults = NSUserDefaults.standardUserDefaults()
        let lastVersion = userDefaults.doubleForKey(key)
        //保存新版本
        userDefaults.setDouble(versionNum!, forKey: key)
        //同步沙盒
        userDefaults.synchronize()
        
        //比较
        return versionNum! > lastVersion
    }
    
    
    
    
    
    
    
    
    
    
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

